/**
 * Copyright (C) 2013 SHANGHAI VOLKSWAGEN, All rights reserved.
 * License version 1.0, a copy of which has been included with this.
 *
 * @File name：com.bwie.auth.service.impl.AuthServiceImpl
 * @Create on：2024/3/22
 * @Author：liuchang
 */
package com.bwie.auth.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bwie.auth.service.AuthService;
import com.bwie.common.constants.Constants;
import com.bwie.common.constants.JwtConstants;
import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.User;
import com.bwie.common.domain.request.LoginRequest;
import com.bwie.common.domain.response.LoginResponse;
import com.bwie.common.exception.AuthException;
import com.bwie.common.result.Result;
import com.bwie.common.utils.JwtUtils;
import com.bwie.system.api.SystemRemoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.UUID;

/**
 * <p>ClassName：AuthServiceImpl</p >
 * <p>Description：</p >
 * <p>Author：liuchang</p >
 * <p>Date：2024/3/22</p >
 */
@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    private SystemRemoteService systemRemoteService;
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public LoginResponse login(LoginRequest loginRequest) {
        Result<User> userResult = systemRemoteService.findByPhone(loginRequest.getPhone());
        if (userResult.getCode()== Constants.ERROR){
            throw new AuthException(userResult.getMsg());
        }
        User user = userResult.getData();
        if (null==user){
            throw new AuthException("用户名不存在，请注册");
        }
        if (!loginRequest.getPassword().equals(user.getUserPwd())){
            throw new AuthException("密码错误");
        }
        String userKey = UUID.randomUUID().toString().replaceAll("-", "");
        HashMap<String, Object> map = new HashMap<>();
        map.put(JwtConstants.USER_KEY,userKey);
        String token = JwtUtils.createToken(map);
        redisTemplate.opsForValue().set(TokenConstants.LOGIN_TOKEN_KEY+userKey, JSONObject.toJSONString(user));
        LoginResponse loginResponse = new LoginResponse();
        loginResponse.setRole(user.getRoleId());
        loginResponse.setToken(token);

        return loginResponse;
    }

    @Override
    public User userInfo() {
        String token = request.getHeader(TokenConstants.TOKEN);
        String userKey = JwtUtils.getUserKey(token);
        String s = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
        User user = JSONObject.parseObject(s, User.class);
        return user;
    }

    @Override
    public Result logOut() {
        String token = request.getHeader(TokenConstants.TOKEN);
        String userKey = JwtUtils.getUserKey(token);
        redisTemplate.delete(TokenConstants.LOGIN_TOKEN_KEY + userKey);
        return Result.success("退出成功");
    }
}
